package com.finance.cooperate.feature.builder.submodel.support.intermediate;

import com.finance.cooperate.common.constant.Constant;
import com.finance.cooperate.common.utils.StringUtil;
import com.finance.cooperate.feature.base.ReaderImpl;
import com.finance.cooperate.feature.core.feature.FeatureDefinition;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName IntermediateFeature003
 * @Description 基础短信统计
 * @Author shenanxin
 * @Date 2023/10/5 10:48
 * @Modify ...
 */
public class IntermediateFeature003 extends ReaderImpl {
    @Override
    public Map<String, String> read(String userId) {

        // 短信普通类特征
        // 不能直接遍历Map，因为这里需要排序
        List<String> timeSplits = Arrays.asList("1w", "2w", "3w", "1m", "2m", "3m");

        Integer lastCount = 0;
        for (String k : timeSplits) {

            String receiveCountStr = super.get(FeatureDefinition.f_sms_basic_v1_.getValue() + "receive_count_" + k);

            if (!StringUtil.isEmptyOrNvl(receiveCountStr)) {

                Integer receiveCount = Integer.parseInt(receiveCountStr);

                Integer currCount = receiveCount - lastCount;

                List<Integer> list = Arrays.asList(0, 10, 30, 50, 100, 150, 200, 500, 900, Integer.MAX_VALUE);
                for (int i = 0; i < list.size(); i++) {

                    Integer minValue = -1;

                    if (i > 0) {
                        minValue = list.get(i - 1);
                    }

                    if (currCount > minValue && currCount <= list.get(i)) {
                        super.put(FeatureDefinition.f_sms_model_v1_.getValue() + "receive_count_" + k + "_" + i, Constant.IS_HIT);
                    }
                }

                lastCount = receiveCount;

            }
        }


        lastCount = 0;
        for (String k : timeSplits) {
            String squareCountStr = super.get(FeatureDefinition.f_sms_basic_v1_.getValue() + "square_count_" + k);
            if (!StringUtil.isEmptyOrNvl(squareCountStr)) {
                Integer squareCount = Integer.parseInt(squareCountStr);
                Integer currCount = squareCount - lastCount;

                List<Integer> list = Arrays.asList(0, 3, 5, 8, 12, 18, 25, 50, 120, Integer.MAX_VALUE);

                for (int i = 0; i < list.size(); i++) {
                    Integer minValue = -1;
                    if (i > 0) {
                        minValue = list.get(i - 1);
                    }

                    if (currCount > minValue && currCount <= list.get(i)) {
                        super.put(FeatureDefinition.f_sms_model_v1_.getValue() + "square_count_" + k + "_" + i, Constant.IS_HIT);
                    }
                }

                lastCount = squareCount;
            }
        }

        lastCount = 0;
        for (String k : timeSplits) {
            String phoneNumCountStr = super.get(FeatureDefinition.f_sms_basic_v1_.getValue() + "phone_num_count_" + k);
            if (!StringUtil.isEmptyOrNvl(phoneNumCountStr)) {
                Integer phoneNumCount = Integer.parseInt(phoneNumCountStr);
                Integer currCount = phoneNumCount - lastCount;

                List<Integer> list = Arrays.asList(0, 3, 6, 9, 15, 25, 50, 90, 120, Integer.MAX_VALUE);
                for (int i = 0; i < list.size(); i++) {
                    Integer minValue = -1;
                    if (i > 0) {
                        minValue = list.get(i - 1);
                    }

                    if (currCount > minValue && currCount <= list.get(i)) {
                        super.put(FeatureDefinition.f_sms_model_v1_.getValue() + "phone_num_count_" + k + "_" + i, Constant.IS_HIT);
                    }
                }

                lastCount = phoneNumCount;

            }
        }


        for (Integer c : Arrays.asList(2, 6, 9, 12, 15, 20, 25, 30, 60)) {

            lastCount = 0;
            for (String k : timeSplits) {

                String sameOppositeCountStr = super.get(FeatureDefinition.f_sms_basic_v1_.getValue() + "same_opposite_" + k + "_" + c);


                if (!StringUtil.isEmptyOrNvl(sameOppositeCountStr)) {

                    Integer sameOppositeCount = Integer.parseInt(sameOppositeCountStr);

                    Integer currCount = sameOppositeCount - lastCount;

                    List<Integer> list = Arrays.asList(0, 3, 7, 10, 15, 20, 30, 50, Integer.MAX_VALUE);

                    for (int i = 0; i < list.size(); i++) {

                        Integer minValue = -1;
                        if (i > 0) {
                            minValue = list.get(i - 1);
                        }

                        if (currCount > minValue && currCount <= list.get(i)) {

                            super.put(FeatureDefinition.f_sms_model_v1_.getValue() + "same_opposite_" + k + "_" + c + "_" + i, Constant.IS_HIT);

                        }

                    }

                    lastCount = sameOppositeCount;
                }
            }
        }


        lastCount = 0;
        for (String k : timeSplits) {
            String sendCountStr = super.get(FeatureDefinition.f_sms_basic_v1_.getValue() + "send_count_" + k);
            if (!StringUtil.isEmptyOrNvl(sendCountStr)) {

                Integer sendCount = Integer.parseInt(sendCountStr);
                Integer currCount = sendCount - lastCount;

                List<Integer> list = Arrays.asList(0, 10, 30, 50, 100, Integer.MAX_VALUE);

                for (int i = 0; i < list.size(); i++) {

                    Integer minValue = -1;

                    if (i > 0) {
                        minValue = list.get(i - 1);
                    }

                    if (currCount > minValue && currCount <= list.get(i)) {

                        super.put(FeatureDefinition.f_sms_model_v1_.getValue() + "send_count_" + k + "_" + i, Constant.IS_HIT);

                    }

                }

                lastCount = sendCount;

            }

        }



        lastCount = 0;
        for (String k : timeSplits) {
            String websiteCountStr = super.get(FeatureDefinition.f_sms_basic_v1_.getValue() + "website_count_" + k);
            if (!StringUtil.isEmptyOrNvl(websiteCountStr)) {
                Integer websiteCount = Integer.parseInt(websiteCountStr);
                Integer currCount = websiteCount - lastCount;

                List<Integer> list = Arrays.asList(0, 2, 6, 11, 24, 39, 54, 84, 148, Integer.MAX_VALUE);

                for (int i = 0; i < list.size(); i++) {
                    Integer minValue = -1;
                    if (i > 0) {
                        minValue = list.get(i - 1);
                    }

                    if (currCount > minValue && currCount <= list.get(i)) {
                        super.put(FeatureDefinition.f_sms_model_v1_.getValue() + "website_count_" + k + "_" + i, Constant.IS_HIT);
                    }
                }

                lastCount = websiteCount;

            }
        }


        lastCount = 0;
        for (String k : timeSplits) {
            String overdueStr = super.get(FeatureDefinition.f_sms_loan_v1_.getValue() + "overdue_" + k);
            if (!StringUtil.isEmptyOrNvl(overdueStr)) {
                Integer overdue = Integer.parseInt(overdueStr);
                Integer currCount = overdue - lastCount;

                List<Integer> list = Arrays.asList(0, 1, 3, 7, Integer.MAX_VALUE);

                for (int i = 0; i < list.size(); i++) {
                    Integer minValue = -1;
                    if (i > 0) {
                        minValue = list.get(i - 1);
                    }

                    if (currCount > minValue && currCount <= list.get(i)) {
                        super.put(FeatureDefinition.f_sms_model_v1_.getValue() + "overdue_" + k + "_" + i, Constant.IS_HIT);
                    }
                }

                lastCount = overdue;

            }
        }




        lastCount = 0;
        for (String k : timeSplits) {
            String loanStr = super.get(FeatureDefinition.f_sms_loan_v1_.getValue() + "loan_" + k);
            if (!StringUtil.isEmptyOrNvl(loanStr)) {
                Integer loan = Integer.parseInt(loanStr);
                Integer currCount = loan - lastCount;

                List<Integer> list = Arrays.asList(0, 1, 5, Integer.MAX_VALUE);

                for (int i = 0; i < list.size(); i++) {
                    Integer minValue = -1;
                    if (i > 0) {
                        minValue = list.get(i - 1);
                    }

                    if (currCount > minValue && currCount <= list.get(i)) {
                        super.put(FeatureDefinition.f_sms_model_v1_.getValue() + "loan_" + k + "_" + i, Constant.IS_HIT);
                    }
                }

                lastCount = loan;

            }
        }


        return super.getResult();
    }
}
